package test.web;

import app.framework.config.FrontGlobalConfig;
import app.framework.front.interceptor.FrontInterceptor;
import app.modules.common.config.AppTableConfig;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import jasmine.framework.context.CurrentSubject;
import jasmine.framework.context.InitSupport;
import jasmine.framework.context.RuntimeProvider;
import jasmine.framework.database.impl.datasource.MultipleDataSource;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.http.CacheControl;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import test.constant.AppTestConstants;
import test.datasource.UnitTestDataSource;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mh.z
 */
public class AppTestConfig implements InitSupport {
    private static final String MAIN_DATA_SOURCE_NAME = "mainDataSource";

    @Override
    public void init(RuntimeProvider runtimeProvider) {
        CurrentSubject.setSubject(AppTestConstants.DEFAULT_TENANT_ID,
                AppTestConstants.DEFAULT_USER_ID);

        // 获取已有数据源
        MultipleDataSource multipleDataSource = (MultipleDataSource) runtimeProvider
                .getByType(DataSource.class);
        DataSource mainDataSource = runtimeProvider.getByName(MAIN_DATA_SOURCE_NAME);

        // 创建单测数据源
        UnitTestDataSource unitTestDataSource = new UnitTestDataSource(mainDataSource);
        AppTestFilter.setUnitTestDataSource(unitTestDataSource);

        Map<Object, Object> dataSourceMap = new HashMap<>();
        dataSourceMap.put(UnitTestDataSource.UNIT_TEST_DATA_SOURCE_NAME, unitTestDataSource);
        // 更新多数据源信息
        multipleDataSource.setTargetDataSources(dataSourceMap);

        ResourceHttpRequestHandler handler = runtimeProvider.getByType(ResourceHttpRequestHandler.class);
        // 设置不缓存静态资源
        handler.setCacheControl(CacheControl.noCache());
    }

    @Bean
    public ServerProperties serverProperties() {
        ServerProperties serverProperties = new ServerProperties();
        serverProperties.getServlet()
                .setContextPath(AppTestConstants.CONTEXT_PATH);

        return serverProperties;
    }

    /**
     * 前端全局配置
     *
     * @return
     */
    @Bean
    public FrontGlobalConfig frontGlobalConfig() {
        return new FrontGlobalConfig();
    }

    /**
     * 前端拦截器
     *
     * @return
     */
    @Bean
    public FrontInterceptor frontInterceptor() {
        return new FrontInterceptor();
    }

    @Bean
    public ObjectMapper objectMapper() {
        Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();
        // 数字序列化成字符串
        builder.featuresToEnable(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);

        ObjectMapper objectMapper = builder.build();
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

        return objectMapper;
    }

    @Bean
    public AppTableConfig appTableConfig() {
        return new AppTableConfig();
    }

}
